/*
 * @Author: liuxinjia 15201611813@163.com
 * @Date: 2023-08-14 15:10:26
 * @LastEditors: liuxinjia 15201611813@163.com
 * @LastEditTime: 2023-08-16 16:35:03
 * @FilePath: \TicketPrintWebPlatformd:\当前项目\马上签金融机构前端项目\gitLab\MashangqianWebByBank\src\utils\OperatorCache.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Axios from '../api/http'
import { Message } from 'element-ui'

let permissions = []
let inited = false

const OperatorCache = {
  info: {},
  inited() {
    return inited
  },
  init(callBack) {
    let infoObj = sessionStorage.getItem('userInfo')
    if (inited) {
      callBack()
    } else if (infoObj && !this.info.Id) {
      this.info = JSON.parse(infoObj)
      callBack()
    } else {
      Axios.post('/Base_Manage/Home/GetBankOperatorInfo').then(resJson => {
        if (resJson.Success) {
          this.info = resJson.Data.UserInfo
          sessionStorage.setItem('userInfo', JSON.stringify(resJson.Data.UserInfo))
          permissions = resJson.Data.Permissions
          inited = true
        } else {
          Message.error('用户信息获取失败')
        }
        callBack()
      })
    }
  },
  hasPermission(thePermission) {
    return permissions.includes(thePermission)
  },
  updateInfo(callback) {
    Axios.post('/Base_Manage/Home/GetBankOperatorInfo').then(resJson => {
      if (resJson.Success) {
        this.info = resJson.Data.UserInfo
        sessionStorage.setItem('userInfo', JSON.stringify(resJson.Data.UserInfo))
        permissions = resJson.Data.Permissions
        inited = true
      } else {
        Message.error('用户信息获取失败')
      }
      callback()
    })
  },
  clear() {
    inited = false
    permissions = []
    this.info = {}
    sessionStorage.removeItem('userInfo')
  }
}

export default OperatorCache
